package require Ttk
package require Img

namespace eval ui {
	bind all <Key-F2> {console show}
    bind all <Key-F3> {console hide}
	
	wm title . "NFK lobby"
	wm attributes . -toolwindow 1
	wm protocol . WM_DELETE_WINDOW {
		if {[tk_messageBox \
                -icon    question \
                -type    yesno \
                -default no \
                -message [::msgcat::mc "Do you want to go?"] \
                -title   [::msgcat::mc "Quit Application?"]] == "yes"} {
			exit
		}
	}

	variable Frames
	
	set Frames(tabs,planet,imgUp) img/tabbar/planet-up
	image create photo $Frames(tabs,planet,imgUp) \
		-file [file join $::currentPath images btn_planet_up.png]
	set Frames(tabs,planet,imgDown) img/tabbar/planet-down
	image create photo $Frames(tabs,planet,imgDown) \
		-file [file join $::currentPath images btn_planet_down.png]
	
	set Frames(tabs,chat,imgUp) img/tabbar/chat-up	
	image create photo $Frames(tabs,chat,imgUp) \
		-file [file join $::currentPath images btn_chat_up.png]
	set Frames(tabs,chat,imgDown) img/tabbar/chat-down
	image create photo $Frames(tabs,chat,imgDown) \
		-file [file join $::currentPath images btn_chat_down.png]
	
	proc create {} {
		variable Frames
		
		. configure -background black
		
		frame .tabbar -background black
		frame .content -background black
		
		set Frames(content,planet) .content.planet
		frame $Frames(content,planet) -background black
		set Frames(content,chat) .content.chat
		frame $Frames(content,chat) -background black
		
		pack .tabbar -fill x -expand no -pady 1
		pack .content -fill both -expand yes -pady 1
		
		set Frames(tabs,planet,button) .tabbar.planet
		label $Frames(tabs,planet,button) -image img/tabbar/planet-up -borderwidth 0
		set Frames(tabs,chat,button) .tabbar.chat
		label $Frames(tabs,chat,button) -image img/tabbar/chat-up -borderwidth 0
		
		bind .tabbar.planet <1> [list [namespace current]::showContent planet .tabbar.planet]
		bind .tabbar.chat <1> [list [namespace current]::showContent chat .tabbar.chat]
		
		pack .tabbar.planet .tabbar.chat -side left -padx 5
	}
	
	proc showContent {who parent} {
		variable Frames
		
		foreach content [array names Frames content,*] {
			catch {
				pack forget $Frames($content)
			}
		}
		
		pack $Frames(content,$who) -fill both -expand yes
		
		foreach tab [array names Frames tabs,*,button] {
			set i [lindex [split $tab ,] 1]
			$Frames($tab) configure -image $Frames(tabs,${i},imgUp)
		}
		
		$Frames(tabs,$who,button) configure -image $Frames(tabs,${who},imgDown)
	}
	
	::hook::add finload [list [namespace current]::showContent planet .tabbar.planet] 100
}